package com.example.petplatform.service;

import com.example.petplatform.dao.PetDao;
import com.example.petplatform.entity.Param.Params;
import com.example.petplatform.entity.Pet;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PetService {
    @Resource
    private PetDao petDao;

    public List<Pet> getAllPets() {
        return petDao.getAllPets();
    }

    public PageInfo<Pet> petSearch(Params params) {
        //开启分页
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        // 获取分页后的数据
        List<Pet> list = petDao.petSearch(params);
        // 返回分页后的数据
        return PageInfo.of(list);
    }

    //添加宠物领养信息
    public List<Pet> petAdd(Pet pet) {
        // 插入新的宠物领养信息记录
        petDao.insertSelective(pet);
        // 返回更新后的宠物领养信息列表
        return petDao.getAllPets();
    }

    //修改宠物领养信息
    public List<Pet> petUpdate(Pet pet) {
        petDao.updateByPrimaryKeySelective(pet);
        return petDao.getAllPets();
    }

    //删除宠物领养信息
    public List<Pet> petDelete(Integer id) {
        petDao.deleteByPrimaryKey(id);
        return petDao.getAllPets();
    }

    /**
     * 获取宠物详情
     */
    public Pet getPetDetail(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("宠物ID不能为空");
        }
        return petDao.getPetDetail(id);
    }
}
